package com.example.xm.ui.home.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.request.RequestOptions;
import com.example.xm.R;
import com.example.xm.adapter.HomeAdapter.VpTabAdapter;
import com.example.xm.base.BaseActivity;
import com.example.xm.bean.HomeBean.HomeYouHomeArticleBean;
import com.example.xm.bean.HomeBean.HomeYouHomeBean;
import com.example.xm.bean.HomeBean.HomeYouHomeExerciseBean;
import com.example.xm.bean.HomeBean.HomeYouHomeSocietyBean;
import com.example.xm.bean.HomeBean.HomeYouHomeTrendBean;
import com.example.xm.interfaces.ILoadData;
import com.example.xm.interfaces.youhome.IYouHome;
import com.example.xm.presenter.home.YouHomePresenter;
import com.example.xm.ui.home.activity.youhome_fragments.ArticleFragment;
import com.example.xm.ui.home.activity.youhome_fragments.DataFragment;
import com.example.xm.ui.home.activity.youhome_fragments.ExerciseFragment;
import com.example.xm.ui.home.activity.youhome_fragments.SocietyFragment;
import com.example.xm.ui.home.activity.youhome_fragments.TrendsFragment;
import com.example.xm.ui.own.activity.DetailEditActivity;
import com.example.xm.utiles.GildeUtils;
import com.example.xm.utiles.ImageFilterUtils;
import com.example.xm.utiles.SpUtils;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;

import java.security.MessageDigest;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class YouHomeActivity extends BaseActivity<IYouHome.Presenter> implements IYouHome.View, ILoadData {

    @BindView(R.id.img_head_bg)
    ImageView imgHeadBg;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.img_shared)
    ImageView imgShared;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.tv_name_you_home)
    TextView tvNameYouHome;
    @BindView(R.id.iv_lv_you_homg)
    ImageView ivLvYouHomg;
    @BindView(R.id.tv_concerned)
    TextView tvConcerned;
    @BindView(R.id.tv_liao)
    TextView tvLiao;
    @BindView(R.id.tv_qun)
    TextView tvQun;
    @BindView(R.id.tv_guanzhu_num)
    TextView tvGuanzhuNum;
    @BindView(R.id.tv_fenshi_num)
    TextView tvFenshiNum;
    @BindView(R.id.tv_jingyan_num)
    TextView tvJingyanNum;
    @BindView(R.id.tv_guanzhu)
    TextView tvGuanzhu;
    @BindView(R.id.tv_fenshi)
    TextView tvFenshi;
    @BindView(R.id.tv_jingyan)
    TextView tvJingyan;
    @BindView(R.id.rl_youhome_info)
    RelativeLayout rlYouhomeInfo;
    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.img_sex)
    ImageView imgSex;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.iv_back2)
    ImageView ivBack2;
    @BindView(R.id.ll_youhome)
    LinearLayout llYouhome;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    private ArrayList<Fragment> fragments;
    private int curPostion;

    @Override
    protected int getLayout() {
        return R.layout.activity_you_home;
    }

    @Override
    protected void initView() {
        tvEdit.setVisibility(View.GONE);
        tvConcerned.setVisibility(View.VISIBLE);
        tvLiao.setVisibility(View.VISIBLE);
        String[] strings = {"资料", "动态", "活动", "社团", "文章"};
        fragments = new ArrayList<>();
        fragments.add(new DataFragment());//资料
        fragments.add(new TrendsFragment());//动态  //网络获取数据显示数据
        //通过activity传值获取数据
        fragments.add(ExerciseFragment.ExerciseFragment(2, this::loadData));//活动
        fragments.add(SocietyFragment.getFragment(3, this::loadData));//社团
        fragments.add(new ArticleFragment());//文章
        //设置tablayout和viewpager
        VpTabAdapter vpTabAdapter = new VpTabAdapter(getSupportFragmentManager(), fragments, strings);
        viewPager.setAdapter(vpTabAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //position就是当前显示的fragment的下标
                curPostion = position;//也可以通过这个下标用来传值给fragment
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tabLayout.setupWithViewPager(viewPager);

    }
    //java代码创建布局
//    @OnClick(R.id.tv_concerned)
//    public void onViewClicked() {
//        Snackbar snackbar = Snackbar.make(layoutSnackbar, "提示信息", Snackbar.LENGTH_SHORT);
//        Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) snackbar.getView();
//        if (snackbarLayout != null) {
//            snackbarLayout.setBackgroundColor(Color.parseColor("#80000000"));
//            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) snackbarLayout.getLayoutParams();
//            if (params == null) {
//                params = new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.WRAP_CONTENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT);
//                params.gravity = Gravity.CENTER_VERTICAL;
//                params.bottomMargin = 50;
//            }
//            snackbar.show();
//        }
//    }


    @Override
    protected void initData() {
        mPresenter.getYouHome();
    }

    @Override
    protected void initListener() {
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivBack2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @SuppressLint({"NewApi", "SetTextI18n"})
    @Override
    public void getYouHomeReturn(HomeYouHomeBean result) {
        if (result.getStatus().getStatusCode() == 100) {
            HomeYouHomeBean.DataBean data = result.getData();
            //设置协调布局背景图
            imgHeadBg.setAlpha(0.5f);
            Glide
                    .with(this)
                    .load(data.getHeadUrl())
                    .apply(RequestOptions.bitmapTransform(new GlideBlurformation(this)))
                    .into(imgHeadBg);

            //设置官方小袍
            tvNameYouHome.setText(data.getNickName());
            //设置等级图标
            int level = data.getLevel();
            if (level == 1) {
                ivLvYouHomg.setImageDrawable(YouHomeActivity.this.getDrawable(R.mipmap.lv1));
            } else if (level == 2) {
                ivLvYouHomg.setImageDrawable(YouHomeActivity.this.getDrawable(R.mipmap.lv2));
            } else if (level == 3) {
                ivLvYouHomg.setImageDrawable(YouHomeActivity.this.getDrawable(R.mipmap.lv3));
            } else if (level == 4) {
                ivLvYouHomg.setImageDrawable(YouHomeActivity.this.getDrawable(R.mipmap.lv4));
            } else if (level == 5) {
                ivLvYouHomg.setImageDrawable(YouHomeActivity.this.getDrawable(R.mipmap.lv5));
            } else if (level == 6) {
                ivLvYouHomg.setImageDrawable(YouHomeActivity.this.getDrawable(R.mipmap.lv6));
            } else if (level == 7) {
                ivLvYouHomg.setImageDrawable(YouHomeActivity.this.getDrawable(R.mipmap.lv7));
            } else if (level == 8) {
                ivLvYouHomg.setImageDrawable(YouHomeActivity.this.getDrawable(R.mipmap.lv8));
            } else if (level == 9) {
                ivLvYouHomg.setImageDrawable(YouHomeActivity.this.getDrawable(R.mipmap.lv9));
            } else {
                ivLvYouHomg.setImageDrawable(YouHomeActivity.this.getDrawable(R.mipmap.lv10));
            }
            //设置同袍官方群
            tvQun.setText(data.getSignature());
            //设置关注数量
            tvGuanzhuNum.setText(data.getMyContact() + "");
            //设置粉丝数量
            tvFenshiNum.setText(data.getContactMe() + "");
            //设置经验度
            tvJingyanNum.setText(data.getExpScore() + "");
            //设置头像
            RequestOptions requestOptions = new RequestOptions().circleCrop();
            Glide.with(YouHomeActivity.this).load(data.getHeadUrl()).apply(requestOptions).into(imgHead);
            //头像大图
            ArrayList<String> imgss = new ArrayList<>();
            imgss.add(data.getHeadUrl());
            imgHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(YouHomeActivity.this, ImageActivity.class);
                    intent.putExtra("postion", 1);
                    intent.putExtra("list", imgss);
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public void getTrends(HomeYouHomeTrendBean result) {

    }

    @Override
    public void getExerciseReturn(HomeYouHomeExerciseBean result) {
        ((ExerciseFragment) fragments.get(2)).initData(result);
    }

    @Override
    public void getSocietyReturn(HomeYouHomeSocietyBean result) {
        ((SocietyFragment) fragments.get(3)).getData(result);
    }

    @Override
    public void getArticleReturn(HomeYouHomeArticleBean result) {
    }

    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visble) {

    }

    @Override
    protected IYouHome.Presenter initPresenter() {
        return new YouHomePresenter();
    }

    @Override
    public void loadData(int pos) {
        switch (pos) {
            case 2:
                mPresenter.getExercise();
                break;
            case 3:
                mPresenter.getSociety();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    static class GlideBlurformation extends BitmapTransformation {

        private Context context;

        public GlideBlurformation(Context context) {
            this.context = context;
        }

        @Override
        protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
            return ImageFilterUtils.instance().blurBitmap(context, toTransform, 20, outWidth, outHeight);
        }

        @Override
        public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

        }
    }


}